from time import timezone, strftime

from django_redis import get_redis_connection
from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo


# class OrderPlaceSerializer(serializers.ModelSerializer):
#     """
#     购物车商品数据序列化器
#     """
#     count = serializers.IntegerField(min_value = 1, label='数量')
#
#     class Meta:
#         model = SKU
#         fields = ('id', 'name', 'default_image_url', 'price', 'count')
#
#
# class OrderCommitSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = OrderInfo
#         fields = ("address","pay_method","order_id")
#
#         read_only_fields = ["order_id"]#序列化的时候，也是返回的时候用，反序列化的时候就不用
#         extra_kwargs ={
#             "address":{
#                 "write_only":True
#             },
#             "pay_method":{
#                 "write_only":True
#             }
#         }

    #1,重写create方法，取出redis数据，创建订单对象，订单商品对象
        # def create(self,validate_data):
        #     #1,获取user
        #     user = self.context["request"].user
        #     #2，创建订单号(尽量保证唯一性)
        #     order_id = timezone.now()strftime(%Y%m%d%H%M%S)
        #     #3，获取地址
        #     OrderInfo.objects.create(
        #         oder_id = order_id,
        #         user = user,
        #         address = address,
        #         total_count = 0,
        #         total_amount = Decimal(10.0),
        #         freight = decimal(10.0),
        #         pay_method = pay_method,
        #         status = "",
        #     )
        #     #7,获取redis数据
        #     redis_conn = get_redis_connection("cart")
        #     cart_dict = redis_conn.htgetall("cart%s")
        #
        #     pass